package com.cd.javacxy.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.cd.javacxy.common.converter.*;
import com.cd.javacxy.common.interceptor.LoginInterceptor;
import com.cd.javacxy.common.utils.Constants;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @author yanghy
 * @version V1.0
 * @date 2019/11/9
 */
@Configuration
public class CxyWebConfig extends WebMvcConfigurationSupport {


    private final LoginInterceptor loginInterceptor;

    public CxyWebConfig(LoginInterceptor loginInterceptor) {
        this.loginInterceptor = loginInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(Constants.EXCLUDE_PATH_PATTERNS);
    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("signin").setViewName("signin");
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/faq").setViewName("faq");
        registry.addViewController("/findpwd").setViewName("findpassword");
        registry.addViewController("/reg").setViewName("register");
        registry.addViewController("/ss").setViewName("shuoshuo");
        registry.addViewController("/about").setViewName("about");
        registry.addViewController("/donate").setViewName("donate");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1、定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2、添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse
        );
        //3、在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //4、将convert添加到converters中
        converters.add(fastConverter);
        //5、追加默认转换器
        super.addDefaultHttpMessageConverters(converters);
    }


    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    protected void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToSolveTypeConverter());
        registry.addConverter(new StringToArticleTypeConverter());
        registry.addConverter(new StringToChooseTypeConverter());
        registry.addConverter(new StringToMessageStatusConverter());
        registry.addConverter(new StringToTopicTypeConverter());
        registry.addConverter(new StringToVoteTypeConverter());
        super.addFormatters(registry);
    }
}
